<?php
namespace app\admin\controller;
use think\Controller;
use app\service\billService;
use app\service\commonService;
use app\service\roominfoService;
use app\service\apiService;
use think\Db;

class Wx extends Controller{
	//获取openid
    public function openid()
    {
        $code = request()->get('code');
        $community_id= request()->get('community_id');
       
        $appid = "";//小程序的唯一标识
        $secret = "";//小程序的AppSecret
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
		$curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);//要访问的地址
        curl_setopt($curl, CURLOPT_HEADER, 1);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//跳过ssl检查项
        curl_setopt($curl, CURLOPT_HEADER, 0);//显示返回的Header区域内容
        $tmpInfo = curl_exec($curl);//执行操作
        // $err = curl_error($curl);//输出错误
        curl_close($curl);//关闭CURL会话
        // $tmpInfo = json_decode(json_encode(simplexml_load_string($tmpInfo, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        $tmpInfo = $this->object_to_array(json_decode($tmpInfo));
        if (!empty($tmpInfo['errmsg'])) {
            return $tmpInfo['errmsg'];
        } else {
            $user['user_id'] = $tmpInfo['openid'];
            return json_encode($user['user_id']);
        }
    }
}